package kuangshen.lock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @program: liangt
 * @description: 测试Lock
 * @author: liangt
 * @create: 2021-03-03 16:14
 */
public class TestLock {
    public static void main(String[] args) {
        Lock2 lock2 = new Lock2();

        new Thread(lock2).start();
        new Thread(lock2).start();
        new Thread(lock2).start();
    }
}

class Lock2 implements Runnable{

    int tickeyNumber = 10;

    //定义可重入锁
    private ReentrantLock reentrantLock = new ReentrantLock();
    @Override
    public void run() {
        while (true){
            try {
                reentrantLock.lock();//枷锁
                if (tickeyNumber > 0){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(tickeyNumber--);
                }else {
                    break;
                }
            }finally {
                reentrantLock.unlock();
            }

        }
    }
}